# -*- coding: utf-8 -*-

import numpy as np
import pandas as pd

from sklearn.utils import shuffle

#得到Type的一级类目
def cutTypeFirst(x):
    n=0
    if x<10:
        n=x
    else:
        n=x//100
    return n
#得到Type的二级类目
def cutTypeSecond(x):
    n=0
    if x<10:
        n=0
    else:
        n=x%100
    return n

def cutTest():
    print('读取 test')
    fi_train=pd.read_csv('data/origin/test.csv')

    fi_train['sort']=[i for i in range(len(fi_train))]

   
    print(' 读入 position')
    fi_pos=pd.read_csv('data/origin/position.csv')
    print('拼接position')
    feature=pd.merge(fi_train,fi_pos,how='left',on='positionID')
    del fi_pos
    del fi_train

    print('读入 ad')
    fi_ad=pd.read_csv('data/origin/ad.csv')
    print('拼接ad')
    feature=pd.merge(feature,fi_ad,how='left',on='creativeID')
    del fi_ad
    
    print('读入 appcategories')
    fi_app_cate=pd.read_csv('data/origin/app_categories.csv')
    print('拼接appcategories')
    feature=pd.merge(feature,fi_app_cate,how='left',on='appID')
    del fi_app_cate
    
    #分割appcategories
    feature['appTypeFirst']=feature['appCategory'].apply(lambda x:cutTypeFirst(x))
    feature['appTypeSecond']=feature['appCategory'].apply(lambda x:cutTypeSecond(x))

    #分割clickTime
    feature['clickday']=feature['clickTime'].apply(lambda x:x//10000)
    feature['clickhour']=feature['clickTime'].apply(lambda x:(x%1000000)//10000)
    feature['clickminute']=feature['clickTime'].apply(lambda x:(x%10000)//100)

    #去掉没用的信息
    '''del feature['']
    del feature['']
    del feature['']'''

    print('开始保存')
    feature.to_csv('data/cutData/test_v1.csv',index=False)
    print('保存完成')

def cutTrain():
    print('读取 trian')
    fi_train=pd.read_csv('data/origin/train.csv')

    fi_train['sort']=[i for i in range(len(fi_train))]

   
    print(' 读入 position')
    fi_pos=pd.read_csv('data/origin/position.csv')
    print('拼接position')
    feature=pd.merge(fi_train,fi_pos,how='left',on='positionID')
    del fi_pos
    del fi_train

    print('读入 ad')
    fi_ad=pd.read_csv('data/origin/ad.csv')
    print('拼接ad')
    feature=pd.merge(feature,fi_ad,how='left',on='creativeID')
    del fi_ad
    
    print('读入 appcategories')
    fi_app_cate=pd.read_csv('data/origin/app_categories.csv')
    print('拼接appcategories')
    feature=pd.merge(feature,fi_app_cate,how='left',on='appID')
    del fi_app_cate
    
    #分割appcategories
    feature['appTypeFirst']=feature['appCategory'].apply(lambda x:cutTypeFirst(x))
    feature['appTypeSecond']=feature['appCategory'].apply(lambda x:cutTypeSecond(x))

    #分割clickTime
    feature['clickday']=feature['clickTime'].apply(lambda x:x//10000)
    feature['clickhour']=feature['clickTime'].apply(lambda x:(x%1000000)//10000)
    feature['clickminute']=feature['clickTime'].apply(lambda x:(x%10000)//100)

    #去掉没用的信息
    '''del feature['']
    del feature['']
    del feature['']'''

    print('开始保存')
    feature.to_csv('data/cutData/feature_v1.csv',index=False)
    print('保存完成')

if __name__=='__main__':
    #cutTrain();
    cutTest();









    
